package com.lz.football.module.court
{
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.MainEvent;
	import com.liangzhong.events.ModuleEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.court.vo.CourtInfoVo;
	import com.liangzhong.model.type.MatchType;
	
	import common.BaseView.BaseView;
	import common.BaseView.DisplayMouseEventManager;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import lz.controls.Alert;
	import lz.controls.LButton;

	/**
	 *球场 
	 * @author Administrator
	 * 
	 */	
	public class CourtView extends BaseView //implements ICourtView
	{
		/**
		 * 背景 
		 */
		private var _bg:MovieClip;
		/**
		 *信息 
		 */
		private var infoMc:MovieClip;
		/**
		 * 
		 */
		private var _courtInfo:CourtInfoVo;
		/**
		 *联赛按钮 
		 */
		public static var leagueBtn:LButton;
		/**
		 *天梯赛按钮 
		 */
		private var ladderBtn:LButton;
//		/**
//		 *好友赛按钮 
//		 */
//		private var friendBtn:SimpleButton;
//		/**
//		 *联盟按钮 
//		 */
//		private var allianceBtn:SimpleButton;
		/**
		 *收入 
		 */
		private var income:TextField;
		
		public function CourtView()
		{
			super();
		}
		/**
		 *背景 
		 * @return 
		 * 
		 */
//		public function get bg():DisplayObject
//		{
//			if(_bg == null)_bg = Reflection.createInstance("com.lz.modul.court.Bg");
//			return _bg;
//		}
		override public function initUI(view:Object=null):void
		{
			symbolSkin = Reflection.createInstance("com.lz.modul.courtMainview");
			this.addChild(symbolSkin);
			income = symbolSkin.income;
			leagueBtn = new LButton(symbolSkin.leagueBtn);
			leagueBtn.enable = true;
			ladderBtn = new LButton(symbolSkin.ladderBtn);
			ladderBtn.enable = true;
//			var arr:Array = [new ColorMatrixFilter([0.5,0.5,0.5,0,0,
//				0.5,0.5,0.5,0,0,
//				0.5,0.5,0.5,0,0,
//				0,0,0,0.8,0,])];
			_courtInfo = AppData.getInstance().courtDataManger.courtInfoVo;
			_windowBg = symbolSkin.courtBg;
			isCloseBtn = true;
			isDrag = true
			moveClose( symbolSkin.width - 30,25);
			_viewHeight = symbolSkin.height;
			_viewWidth = symbolSkin.width;
			onResizeHandler();
			addEvtListener();
			setLadderInfo();
//			showBaseInfo();
		}
		/**
		 *设置天梯信息 
		 * 
		 */
		private function setLadderInfo():void
		{
//			AppData.getInstance().ladderDataManger.ladderInfovo
			symbolSkin.ladderBtn.infoMc.rank.gotoAndStop(AppData.getInstance().ladderDataManger.ladderInfovo.ladderlevel);
			symbolSkin.ladderBtn.infoMc.des.text = "天梯积分：" + AppData.getInstance().ladderDataManger.ladderInfovo.ladderintegral + "\n"+
				"天梯荣誉：" + AppData.getInstance().ladderDataManger.ladderInfovo.honour + "\n"+
				"当前排名：" + levelChange() + "级第"+AppData.getInstance().ladderDataManger.ladderInfovo.ladderrank + "名\n"+
				"当天可挑战数：" + AppData.getInstance().ladderDataManger.ladderInfovo.laddercount;
			income.text =""//"当前门票收入："+AppData.getInstance().ladderDataManger.ticketIncome() + "欧元";
			income.width = income.textWidth + 5;
			
//			DisplayMouseEventManager.instance.addTipsDisplay(income,"俱乐部等级越高门票收入越多！");
			if(AppData.getInstance().clubDatamanager.clubInfo.level < 10)
			{
				ladderBtn.enable = false;
				symbolSkin.ladderBtn.infoMc.des.text = "俱乐部等级10级开放天梯赛";
			}
			var ntype :int = AppData.getInstance().leagueMatchManager.ntype;
			symbolSkin.leagueBtn.infoMc.des.text = "当前进度："+leagueType()+"\n\n联赛可以获得大量声望！";
//			ladderBtn.enable = false;
//			symbolSkin.ladderBtn.infoMc.des.text = "俱乐部等级10级开放天梯赛";
		}
		private function leagueType():String
		{
			switch(AppData.getInstance().leagueMatchManager.ntype)
			{
				case MatchType.PRE_MATCH://预选赛
				
					return "预选赛";
					break;
				case MatchType.BASE_MATCH://基础联赛
					return "基础联赛";
					break;
				case MatchType.CHAMPION_MATCH://冠军联赛
					return "冠军联赛";
				
					break;
				case MatchType.LEGEND_MATCH://传奇
					return "传奇";
					break;
			}
			return "";
		}
		/**
		 *天梯等级 
		 * @return 
		 * 
		 */
		private function levelChange():String
		{
			switch(AppData.getInstance().ladderDataManger.ladderInfovo.ladderlevel)
			{
				case 1:
					return "D";
				case 2:
					return "C";
				case 3:
					return "B";
				case 4:
					return "A";
				case 5:
					return "S";
			}
			return "D";
		}
		override public function addEvtListener():void
		{
			super.addEvtListener();
			leagueBtn.addEventListener(MouseEvent.CLICK,onMatchtypeHandler);
			ladderBtn.addEventListener(MouseEvent.CLICK,onMatchtypeHandler);
			
			
//			friendBtn.addEventListener(MouseEvent.CLICK,onMatchtypeHandler);
//			allianceBtn.addEventListener(MouseEvent.CLICK,onMatchtypeHandler);
		}
		
		/**
		 * 点击不同的按钮状态 
		 * @param event
		 * 
		 */
		protected function onMatchtypeHandler(event:MouseEvent):void
		{
			switch(event.target)
			{
				case leagueBtn:
					AppEventDispatcher.getInstance().dispatchEvent(new MainEvent(MainEvent.OPEN_LEAGUEMATCH_VIEW));
					break;
				case ladderBtn:
					AppEventDispatcher.getInstance().dispatchEvent(new MainEvent(MainEvent.OPEN_LADDER_VIEW));
					break;
//				case friendBtn:
//					
//					break;
//				case allianceBtn:
//					
//					break;
			}
		}
		
//		/**
//		 *升级球场 
//		 * @param event
//		 * 
//		 */
//		protected function onUpGradeHandler(event:MouseEvent):void
//		{
//			// TODO Auto-generated method stub
//			if(_courtInfo.coin > AppData.getInstance().clubDatamanager.clubInfo.coin)
//			{
//				Alert.showErrorTip("欧元不足，不可以升级");
//				return ;
//			}
//			if(_courtInfo.needlevel > AppData.getInstance().clubDatamanager.clubInfo.level)
//			{
//				Alert.showErrorTip("欧元不够，不可以升级");
//				return ;
//			}
//			Alert.show("升级需要消耗" +_courtInfo.coin+"欧元","",Alert.NO,GlobalDef.AlertContainer,upGradeAlert);
//			function upGradeAlert(type:int):void
//			{
//				if(type == Alert.YES)
//					AppEventDispatcher.getInstance().dispatchEvent(new CourtEvent(CourtEvent.UP_COURT));
//			}
//			
//		}

//		protected function onDropTimeHandler(event:TextEvent):void
//		{
//			// TODO Auto-generated method stub
//			var info :String = "您确定花费200点券去除冷却时间?";
//			Alert.show(info,"",Alert.YES | Alert.NO,GlobalDef.AlertContainer,dropTimeAlertHandler);
//		}
//		/**
//		 * 去除冷却时间 确认
//		 * @param type
//		 * 
//		 */		
//		private function dropTimeAlertHandler(type :int):void
//		{
//			if(type == Alert.YES)
//			{
//				AppEventDispatcher.getInstance().dispatchEvent(new CourtEvent(CourtEvent.CLEAR_UP_TIME));
//			}
//		}
//		private function onTiming():void
//		{
			// TODO Auto Generated method stub
//			leftTime --;
//			if(leftTime <= 0)
//			{
//				txtLeftTime.visible = false;
//				txtTimeDesc.visible = false;
//				upLvBtn.visible = true;
//				TimerManager.instance.removeFunction(onTiming);
//			}
//			else
//			{
//				txtLeftTime.htmlText = "<u><a href='event:dropTime'>" + DateUtil.formatShowHMS(leftTime,true) + "</a><u>";
//			}
//		}
		/**
		 *关闭 
		 * @param event
		 * 
		 */
		override public function onCloseHandler(event:MouseEvent):void
		{
			symbolSkin.mouseEnabled = false;
			symbolSkin.mouseChildren = false;
			this.dispatchEvent(new ModuleEvent(ModuleEvent.REMOVE_MODULE,true));
		}
		/**
		 *离开确认 
		 * @param type
		 * 
		 */		
		private function closeAlertHandler(type :int):void
		{
			if(Alert.YES == type)
			{
				symbolSkin.mouseEnabled = false;
				symbolSkin.mouseChildren = false;
				this.dispatchEvent(new ModuleEvent(ModuleEvent.REMOVE_MODULE,true));
				super.onCloseHandler(null);
			}
		}
		/**
		 * 
		 * 
		 */
		override public function removeEvtListener():void
		{
			super.removeEvtListener();
			leagueBtn.removeEventListener(MouseEvent.CLICK,onMatchtypeHandler);
			ladderBtn.removeEventListener(MouseEvent.CLICK,onMatchtypeHandler);
			
//			friendBtn.removeEventListener(MouseEvent.CLICK,onMatchtypeHandler);
//			allianceBtn.removeEventListener(MouseEvent.CLICK,onMatchtypeHandler);
		}
		/**
		 * 
		 * 
		 */
		override public function destroyView():void
		{
//			if(this.parent)
//			{
//				this.parent.removeChild(this);
//			}
			isDrag = false;
			removeEvtListener();
			leagueBtn.enable = false;
			leagueBtn = null;
			ladderBtn.enable = false;
			ladderBtn = null;
			_bg = null;
			_courtInfo = null;
			income = null;
			super.destroyView();
			DisplayObjectUtil.removeAllChildren(this);
		}
		public function clearTime($obj:Object):void
		{
//			showBaseInfo();
		}
		
		public function courtUp($obj:Object):void
		{
//			showBaseInfo();
		}
	}
}